package service

import (
	"GIN-API/internal/models"
	"GIN-API/internal/models/DTO"
	"GIN-API/internal/models/VO"
	"GIN-API/internal/repository"
	"gorm.io/gorm"
)

// ArticleService 定义文章服务层结构体
type ArticleService struct {
	articleRepo *repository.ArticleRepository
	coverRepo   *repository.CoverRepository
}

// NewArticleService 创建文章服务层实例
func NewArticleService(articleRepo *repository.ArticleRepository, coverRepo *repository.CoverRepository) *ArticleService {
	return &ArticleService{
		articleRepo: articleRepo,
		coverRepo:   coverRepo,
	}
}

// CreateArticle 创建文章
func (as *ArticleService) CreateArticle(articleRequest *DTO.CreateArticleRequest) error {
	// 先创建封面
	cover := &models.Cover{
		ImageURL: articleRequest.CoverURL,
		Type:     articleRequest.CoverType,
	}
	// 获取cover的id
	createdCover, err := as.coverRepo.CreateCover(cover)
	if err != nil {
		return err
	}
	// 再创建文章
	article := &models.Article{
		Draft:     articleRequest.Draft,
		Title:     articleRequest.Title,
		Content:   articleRequest.Content,
		AuthorID:  articleRequest.AuthorID,
		ChannelId: articleRequest.ChannelID,
		CoverId:   createdCover.ID,
	}
	return as.articleRepo.CreateArticle(article)
}

// GetArticleByID 根据文章 ID 获取文章信息
func (as *ArticleService) GetArticleByID(id uint) (*VO.ArticleVO, error) {
	var articleVO *VO.ArticleVO
	article, err := as.articleRepo.GetArticleByID(id)
	if err != nil {
		return nil, err
	}
	if article != nil {
		cover, err := as.coverRepo.GetCoverByID(article.CoverId)
		if err != nil {
			return nil, err
		}
		articleVO = &VO.ArticleVO{
			Article: article,
			Cover:   cover,
		}
	}
	return articleVO, nil
}

// UpdateArticle 更新文章信息
func (as *ArticleService) UpdateArticle(articleReq *DTO.UpdatedArticleRequest) error {
	// 先修改文章
	article := &models.Article{
		Model:     gorm.Model{ID: articleReq.ID},
		Draft:     articleReq.Draft,
		Title:     articleReq.Title,
		Content:   articleReq.Content,
		ChannelId: articleReq.ChannelID,
		AuthorID:  articleReq.AuthorID,
	}
	coverId, err := as.articleRepo.UpdateArticle(article) // 获取更新后的 coverId
	if err != nil {
		return err
	}

	// 更新封面数据
	cover := &models.Cover{
		Model:    gorm.Model{ID: coverId},
		ImageURL: articleReq.CoverURL,
		Type:     articleReq.CoverType,
	}
	if err := as.coverRepo.UpdateCover(cover); err != nil {
		return err
	}

	return nil
}

// DeleteArticle 根据文章 ID 删除文章
func (as *ArticleService) DeleteArticle(id uint) error {
	err, coverId := as.articleRepo.DeleteArticle(id)
	if err != nil {
		return err
	}
	if err := as.coverRepo.DeleteCover(coverId); err != nil {
		return err
	}
	return err
}

// GetAllArticles 获取所有文章
func (as *ArticleService) GetAllArticles(req *DTO.ListArticleRequest, authorID uint) ([]*VO.ListArticleVO, int64, error) {
	articles, total, err := as.articleRepo.GetAllArticles(req, authorID)
	if err != nil {
		return nil, 0, err
	}
	var listArticleVO []*VO.ListArticleVO
	for _, article := range articles {
		cover, err := as.coverRepo.GetCoverByID(article.CoverId)
		if err != nil {
			return nil, 0, err
		}
		listArticleVO = append(listArticleVO, &VO.ListArticleVO{
			ID:           article.ID,
			UpdatedAt:    article.UpdatedAt,
			Draft:        article.Draft,
			Title:        article.Title,
			Content:      article.Content,
			AuthorID:     article.AuthorID,
			ChannelId:    article.ChannelId,
			CoverId:      article.CoverId,
			ReadCount:    article.ReadCount,
			CommentCount: article.CommentCount,
			LikeCount:    article.LikeCount,
			StarCount:    article.StarCount,
			CoverUrl:     cover.ImageURL,
		})
	}
	return listArticleVO, total, nil
}
